/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spelet;

import java.util.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 *
 * @author Tom
 */
public class Game extends Canvas {

    public static int square = 40;
    private BufferStrategy strategy;
    private boolean gameRunning = true;
    boolean leftPressed;
    boolean rightPressed;
    boolean upPressed;
    boolean downPressed;
    boolean wPressed;
    boolean aPressed;
    boolean sPressed;
    boolean dPressed;
    boolean enterPressed;
    boolean menuActive=true;
    boolean keyAction = false;
    boolean editor = true;
    boolean controlFrame=false;
    boolean firstAction=false;
    String lvl = "editor";
    int menuIndex =1;
    int spawnX=0;
    int spawnY=0;
    int frameCount=1;

   JPanel panel;
   JFrame container;
    public Game() {
        container = new JFrame();

        panel = (JPanel) container.getContentPane();
        panel.setPreferredSize(new Dimension(800, 600));
        panel.setLayout(null);
        setBounds(0, 0, 800, 600);
        panel.add(this);


        setIgnoreRepaint(true); //Säger till awt att inte måla om canvasen,
                                //eftersom vi gör de i "accelerated mode"
        container.pack();
        container.setResizable(false);
        container.setVisible(true);

        //Låter awt att använda accelerarad grafik
        createBufferStrategy(2);
        strategy = getBufferStrategy();

        addKeyListener(new KeyInputHandler());
        requestFocus();
    }
 

    public void gameLoop() {

        long lastLoopTime = System.currentTimeMillis();
        java.util.List<Menu> menuList = new ArrayList<Menu>();
        java.util.List<Entity> entities = new ArrayList<Entity>();
                                                //Lägger in de olika knapparna
        menuList.add(new Menu("play", 350, 100));
        menuList.add(new Menu("level", 350, 200));
        menuList.add(new Menu("editor", 350, 300));
        menuList.add(new Menu("quit", 350, 400));

        Hero hero = new Hero(0,0);

        
        while (gameRunning) {
            long delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();

            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
            g.setColor(Color.black);
            g.fillRect(0, 0, 800, 600);
            ///////////////////////////////////////////////////////////

            if(menuActive==true){
                menu(menuList,entities,hero,g);
                //System.out.print("true");
            }

            if (menuActive == false) {
                //////////////////////////////////////////////////////////////
                /////////////////////////////////////////////////////////////
                if (controlFrame == true) {
                    if ((leftPressed) && (!rightPressed)){
                        hero.run(entities,-1);
                        firstAction=true;

                    } else if ((!leftPressed) && (!rightPressed) || (leftPressed) && (rightPressed)) {
                        hero.run(entities,0);

                    } else if ((rightPressed) && (!leftPressed)){
                        hero.run(entities,1);
                         firstAction=true;
                    }

                    if ((upPressed) && (!downPressed) && (!keyAction)) {
                        hero.jump(entities);
                        keyAction=true;
                        firstAction=true;

                    } else if ((downPressed) && (!upPressed) && (!keyAction)) {
                    }
                
                //////////////////////////////////////////////////////////////
                //////////////////////////////////////////////////////////////
                if((hero.collisionUnder(entities,0)==false) && firstAction==true){
                   hero.gravity(entities);
                    
                }
                }

                //ritar  ut alla entitys
                for (Entity f : entities) {
                    f.draw(g);
                }
                hero.draw(g);
            }
            if(controlFrame==true){
                controlFrame=false;
            }
            else{
                controlFrame=true;
            }
            ////////////////////
            g.dispose();
            strategy.show();
            frameCount++;
            //Stabiliserar framsen genom att pausa lite
            try {
                Thread.sleep(12);
            } catch (Exception e) {
            }

        }
    }

    public static void main(String argv[]) {
        Game g = new Game();
        g.gameLoop();
    }

    //Huvudmenyn
    private void menu(java.util.List<Menu> menuList,java.util.List<Entity> entities,
            Hero hero,Graphics2D g) {
        for (Menu f : menuList) {
            f.selected = false;
        }
        if ((upPressed) && (!downPressed) && (!keyAction)) {
            keyAction = true;
            menuIndex--;
            if (menuIndex == 0) {
                menuIndex = 4;
            }
        }

        if ((!upPressed) && (downPressed) && (!keyAction)) {
            keyAction = true;
            menuIndex++;
            if (menuIndex == 5) {
                menuIndex = 1;
            }
        }
        //Vad som händer vid de olika knapparna
        if (menuIndex == 1) {
            menuList.get(0).selected = true;
            if ((enterPressed) && (!keyAction)) {
                System.out.println("pressed");
                keyAction=true;
                menuActive=false;
                loadLvl("story1",entities,hero);
                //backgroundMusic = soundList.getClip(music);
                //backgroundMusic.play();
                
            }
        }
        else if (menuIndex == 2) {
            menuList.get(1).selected = true;
            if ((enterPressed) && (!keyAction)) {
                keyAction=true;
            }
        }
        else if (menuIndex == 3) {
            menuList.get(2).selected = true;
            if ((enterPressed) && (!keyAction)) {
                keyAction=true;
                Editor e = new Editor();
                container.setVisible(false);
                e.gameLoop();


            }
        }
        else if (menuIndex == 4) {
            menuList.get(3).selected = true;
            if ((enterPressed) && (!keyAction)) {
                keyAction=true;
                System.exit(0);
            }
        }

            //Ritar ut menyn
        for (Menu f : menuList) {
            f.draw(g);
        }
    }
    //Laddar bana från angivet bannamn
    private void loadLvl(String lvl,java.util.List<Entity> entities,Hero hero){
    File file = new File("maps\\"+lvl+".txt");
    String mapName;
    String tile;
    int x;
    int y;


        try {
            // Skapar en scanner som läser från vald textfil
            Scanner sc;
            sc = new Scanner(file);
            mapName = sc.next();
            while (sc.hasNext()) {
                tile = sc.next();
                x = sc.nextInt();
                y = sc.nextInt();


                if (tile.equals("brick")) {
                    entities.add(new Brick(x, y));

                }
                else if(tile.equals("tnt")){
                    entities.add(new Tnt(x,y));
                }
                else if(tile.equals("rust")){
                    entities.add(new Rust(x,y));
                }
                //Skapar "hjälten" på koordinaterna man placerat spawn-flaggan
                //i edditorn.
                else if (tile.equals("spawn")) {
                    hero.position_x=x;
                    hero.position_y=y;
                }
            }
            sc.close();
        }

        catch (FileNotFoundException e) {
            System.out.println("File not found!");
            // Stop program if no file found
            System.exit(0);
 
        }
    //System.out.print("Maahahahaa");

    }
/////////////////////////////////////////////////////////////

    class KeyInputHandler extends KeyAdapter {

        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                leftPressed = true;
            }

            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                rightPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                upPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                downPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_W) {
                wPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_A) {
                aPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_S) {
                sPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_D) {
                dPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                enterPressed = true;
            }
        }

        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                leftPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                rightPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                upPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                downPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_W) {
                wPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_A) {
                aPressed = false;
                keyAction = false;
                ;
            }
            if (e.getKeyCode() == KeyEvent.VK_S) {
                sPressed = false;
                keyAction = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_D) {
                dPressed = false;
                keyAction = false;
            }

            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                enterPressed = false;
                keyAction = false;
            }
        }

        public void keyTyped(KeyEvent e) {
            // if we hit escape, then quit the game
            if (e.getKeyChar() == 27) {
                System.exit(0);
            }
        }
    }
}
